package com.woke.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bean.BankBranchCode;
import com.jakewharton.rxbinding2.view.RxView;
import com.zhongcai.online.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewProvider;

/**
 * 作者 ： 暗网
 * 创建时间：2017/5/10
 * 备注： 省市
 */

public class BankBranchViewProvider extends ItemViewProvider<BankBranchCode, BankBranchViewProvider.ViewHolder> {

    private OnItemClickListener mItemClickListener;

    public BankBranchViewProvider(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.area_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final BankBranchCode bankCode) {

        holder.textView.setText(bankCode.getBranchname());
        RxView.clicks(holder.itemView).throttleFirst(2, TimeUnit.SECONDS).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                if (mItemClickListener != null) {
                    mItemClickListener.OnItemClick(holder.itemView, bankCode, holder.getAdapterPosition());
                }
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
        }
    }
}
